﻿@model PagingDataSet<Comment>
@{
    var commentedObjectId = ViewData.Get<long>("commentedObjectId", 0);
    //var commentCount = ViewData.Get<int>("commentCount", 0);
    //var disapprovedCount = ViewData.Get<int>("disapprovedCount", 0);
    var tenantTypeId = ViewData.Get("tenantTypeId", string.Empty);
    var userId = ViewData.Get<long>("userId",0);
    var isOnlyAuthor = ViewData.Get<bool>("isOnlyAuthor", false);
    var pageIndex = ViewData.Get("pageIndex", 1);
    int floor = (pageIndex - 1) * 10 + 1;
    var _currentUser = UserContext.CurrentUser;
    Authorizer authorizer = new Authorizer();
}
@if (Model != null && Model.TotalRecords > 0)
{
    foreach (var comment in Model)
    {
        <div class="media">
            <div class="media-left">
                @Html.ShowUserAvatar(userId: comment.UserId, avatarSizeType: AvatarSizeType.Small)
            </div>
            <div class="media-body commentContent" name="commentdetail-@comment.Id" id="commentdetail-@comment.Id" data-id="@comment.Id">
                <h4 class="media-heading"><a href="@SiteUrls.Instance().MyHome(comment.UserId)">@comment.Author</a><small class="pull-right">#@floor</small></h4>
                <p>@Html.Raw(comment.Body)</p>
                <div class="tn-feedback ">
                    <small class="pull-left text-muted">@comment.DateCreated.ToFriendlyDate()</small>
                    <ul id="operation" class="list-inline text-right text-muted" onmouseover="$.fn.showoperation(this);" onmouseout="$.fn.hideoperation(this);">
                        @if (_currentUser != null)
                        {
                            if (_currentUser.UserId == comment.UserId || authorizer.IsPostManager(_currentUser))
                            {
                                <li><a href="javascript:;" class="delete" name="deletecomment" data-id="@comment.Id" style="display:none"><i class="fa fa-trash"></i> 删除</a></li>
                            }
                            <li ><a href="javascript:;" name="report"   data-url="@CachedUrlHelper.Action("_EditImpeachReport", "Common", null, new RouteValueDictionary { { "tenantTypeId", TenantTypeIds.Instance().Comment() }, { "reportObjectId", comment.Id } , { "subject", StringUtility.Trim(comment.Body,15)}})"><i class="fa fa-exclamation-circle"></i> 举报</a></li>


                            bool? isSupport = comment.IsSupport(_currentUser.UserId);
                            var supportCount = comment.GetSupportCount();

                            if (isSupport.HasValue && isSupport.Value)
                            {
                                <li><a href="javascript:;"><i class="fa fa-thumbs-o-up"></i> 已赞(<span id="SupportCount-@comment.Id">@supportCount</span> )</a></li>
                            }
                            else
                            {
                                <li id="commentsupport-@comment.Id"><a href="javascript:;"><i class="fa fa-thumbs-o-up"></i> 赞(<span id="SupportCount-@comment.Id">@supportCount</span>)</a></li>

                            }
                            <li><a href="javascript:;" class="reply" data-href="@CachedUrlHelper.Action("_Comment", "Post", null, new RouteValueDictionary { { "parentId", comment.Id }, { "commentedObjectId", commentedObjectId } })" data-isshow="0"><i class="fa fa-reply"></i> 回复</a></li>
                        }
                        else
                        {
                            <li></li>
                        }
                    </ul>
                    <div class="_ChildrenComment" style="margin-top:10px">
                        @Html.Action("_ChildrenComment", "Post", new { parentId = comment.Id, commentedObjectId = commentedObjectId ,pageIndex=1})
                    </div>
                </div>
            </div>

        </div>
        floor++;
    }
    <div class="text-center">
        @Html.AjaxPagingButton(Model, "_ListComment", CachedUrlHelper.Action("_ListComment", "Post", null, new RouteValueDictionary { { "threadId", commentedObjectId }, { "isOnlyAuthor", isOnlyAuthor } }))
    </div>
}
else
{
    <div class="media">
        <div class="media-body">
            <h4 class="media-heading text-warning text-center">暂无评论</h4>
        </div>
    </div>
}


<script type="text/javascript">
    require(['jquery', 'tnlayer','page'], function ($, tnlayer) {
        $("[id^='commentsupport-']").click(function myfunction() {
            var objectId = $(this).attr('id').replace(/[^0-9]/ig, "");
            $.post("@CachedUrlHelper.Action("Attitude", "Common")",{"tenantTypeId":@tenantTypeId,"objectId":objectId},function myfunction(data) {
                if(data.state){
                    layer.msg("点赞成功", { icon: 1 });
                    var SupportCount = parseInt($("#SupportCount-"+objectId).html());
                    SupportCount++
                    $("#commentsupport-"+objectId).html('<a href="javascript:;"><i class="fa fa-thumbs-o-up"></i> 已赞(<span id="SupportCount-+"'+objectId+'>'+SupportCount+'</span>)</a>');
                    $("#commentsupport-"+objectId).attr("id","");
                }
                else {
                    layer.msg("请先登录", { icon: 0});
                }
            })
        });


        //悬停显示删除
        $.fn.showoperation = function (e) {
            var commentdel = $(e).find("a");
            if (commentdel.length >  3) {

                commentdel[0].style.display = "inline";
                commentdel[1].style.display = "inline";

            }else if (commentdel.length > 2) {
                commentdel[0].style.display = "inline";
            }
        }
        $.fn.hideoperation = function (e) {
            var commentdel = $(e).find("a");
            if (commentdel.length > 3) {
                commentdel[0].style.display = "none";
                commentdel[1].style.display = "none";

            }else if (commentdel.length > 2) {
                commentdel[0].style.display = "none";
            }
        }

        //删除
        $('.delete').on("click", function () {
            var $this=$(this);
            layer.confirm("是否删除", { btn: ['确认', '取消'] }, function () {
                $.post("@CachedUrlHelper.Action("DeleteComment", "Post")", { commentId:$this.data('id')}, function (data) {
                    if (data.state) {
                        layer.msg("删除成功", {
                            icon: 1
                        })
                        $.get("@CachedUrlHelper.Action("_ListComment", "Post")",{ threadId:@commentedObjectId ,userId:@userId,tenantTypeId:@tenantTypeId},function(data){
                            $('#_ListComment').html(data);
                        });
                    }
                });
            })
        })})
</script>
